//
//  Reset.m
//  MUBATE
//
//  Created by sunbin on 2017/2/15.
//  Copyright © 2017年 Quanli. All rights reserved.
//

#import "Reset.h"
#import "ResetPwdView.h"
#import "EnterAccount.h"
#import "Login.h"
#import "ResetModel.h"
@interface Reset ()

@end

@implementation Reset
{
    ResetPwdView *container;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    WEAKSELF;
    [weakSelf touchEven];
    container = (ResetPwdView *)[NHUtils loadViewFormCtl:weakSelf view:[ResetPwdView LoadResetPwdView]];

    container.nextStepHandler = ^(NSString *account ,NSInteger make ,pwdErrorType ty){
        switch (make) {
            case 0:{
                [NHUtils presentViewController:@"EnterAccount" ctl:weakSelf animationType:WXSTransitionAnimationTypeSysPushFromLeft isSysBackAnimation:YES];
            }
                break;
            case 1:{
                [weakSelf resetPwd:account pwdErrorType:ty];
            }
                break;
            default:
                break;
        }
    };
}


- (void)resetPwd:(NSString *)pwd pwdErrorType:(pwdErrorType)ty{
    WEAKSELF;
    [self.view showLoading:@""];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.view hideHUD];
        if (ty == NOError) {
            [ResetModel resetHandle:ResetPwdOperate handleValue:pwd handleAccount:weakSelf.account accountTy:weakSelf.ty success:^(NSDictionary *loginresults, NSString *loginRequestmsg) {
                
                // 返回登录页面 传账号
                Login *login = [[Login alloc] init];
                login.account = weakSelf.account;
                [weakSelf presentViewController:login animated:NO completion:^{
                    [login.view showLoadFinish:loginRequestmsg showLoc:bottom];
                }];
                
            } failure:^(NSDictionary *loginresults, responseState resMake, NSString *loginRequestError) {
                [NHUtils alertAction:@selector(pwderror) alertControllerWithTitle:@"错误" Message:loginRequestError Vctl:weakSelf Cancel:NO];
            }];
        }else{
            NSString *message = ty==FormatError?@"密码格式错误":@"两次输入密码不相同,请重新输入";
            [NHUtils alertAction:@selector(pwderror) alertControllerWithTitle:@"错误" Message:message Vctl:weakSelf Cancel:NO];
        }
    });
}


- (void)pwderror{
    // 清理数据
    container.pwd.text = nil;
    [container.pwd becomeFirstResponder];
    container.confirmBtn.enabled = NO;
}


#warning bug 父类已经添加点击手势
- (void)touchEven{
    WEAKSELF;
    weakSelf.view.backgroundColor = [UIColor blackColor];
    
    [self.view addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [weakSelf.view endEditing:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
